import { fileURLToPath } from 'url'
import nunjucks from 'nunjucks'

export const registryTemplate = function () {
    return async (ctx, next) => {
        ctx.render = function (html, obj) {

            let currentFileUrl = import.meta.url;

            let viewsBaseUrl = new URL('../views', currentFileUrl);
            let viewsBasePath = fileURLToPath(viewsBaseUrl);

            let env = nunjucks.configure(viewsBasePath, { noCache: true, watch: true });
            ctx.body = env.render(html, obj)

        }
        await next()
    }
}